<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@page import="com.wowangz.cms.common.utils.MessageObject"%>
<%-- 引入页面头 --%>
<%@ include file="/pageHead.jsp"%>
<script src="${systemctx}/resources/ckeditor/ckeditor.js"></script>
<script src="${systemctx}/resources/ckeditor/config.js?v=1"></script>
<form name="form1" id="form1" action="" method="post">
	<input type="hidden" id="id" name="id" value="${emailSet.id}">
	<input type="hidden" id="contentFilter" name="contentFilter" value="<%=SysConstant.YesOrNo.NO %>">
	<div class="pageMain">
		<div class="pageTitle">
		<%=MessageResourcesUtils.getMessage(request, "systemmanage.bascsetting.title") %>
		<span></span>
		<%=MessageResourcesUtils.getMessage(request, "systemmanage.email.title") %>
		<span></span>
		<%=MessageResourcesUtils.getMessage(request, "systemmanage.email.edit.title") %></div>
		<div class="pageBody">
			<table class="formTableCommStyle" id="editTable">
				<tr>
					<td class="TableTd1">
						<i>&nbsp;</i>
						<%=MessageResourcesUtils.getMessage(request, "systemmanage.email.column.templatecode") %>
					</td>
					<td class="TableTd2" colspan="3">
						<input type="text" readonly="readonly" value="${emailSet.templatecode}" id="templatecode" name="templatecode" />
					</td>
				</tr>
				<tr>
					<td class="TableTd1">
						<i>*</i>
						<%=MessageResourcesUtils.getMessage(request, "systemmanage.email.column.templatetype") %>
					</td>
					<td class="TableTd2" colspan="3">
						<input type="text" value="${emailSet.subject }" name="subject" id="subject" class="validate[required,maxSize[249]]">
					</td>
				</tr>
				<tr>
					<td class="TableTd1">
						<i>*</i>
						<%=MessageResourcesUtils.getMessage(request, "systemmanage.email.column.subject") %>
					</td>
					<td class="TableTd2" colspan="3">
						<select id="templatetype" name="templatetype" class="validate[required]"></select>
					</td>
				</tr>
				<tr>
					<td class="TableTd1">
						<i>&nbsp;</i>
						<%=MessageResourcesUtils.getMessage(request, "systemmanage.email.column.emailparameter") %>
					</td>
					<td class="TableTd2" colspan="3">
						<span class="smsparameter"></span>
					</td>
				</tr>
				<tr>
					<td class="TableTd1" colspan="4">
						<i>*</i>
						<%=MessageResourcesUtils.getMessage(request, "systemmanage.email.column.tamplatecontent") %>
					</td>
				</tr>
				<tr>
					<td class="TableTd1" colspan="4">
						<textarea cols="50" name="templatecontent" style="display: none;" id="templatecontent"></textarea>
						<textarea id="editor01">${emailSet.templatecontent}</textarea>
						<script type="text/javascript">
							CKEDITOR.replace('editor01');
						</script>
					</td>
				</tr>
			</table>
        	<div class="button-group">
       			<input type="hidden"  name="updateposition" id="updateposition" value="0"/>
       			<input type="button" class="btn btn-default"  id="butSave" name="butSave" value='<%=MessageResourcesUtils.getMessage(request, "common.button.update") %>' />
       			<input type="button" class="btn btn-default"  id="butReturn" name="butReturn" value='<%=MessageResourcesUtils.getMessage(request, "common.button.back") %>' />
			</div>
		</div>
	</div>
</form>

<script type="text/javascript">	
	$(document).ready(function() {
		$("#templatetype").selectutil({
			optModel :<%=SysConstant.TemplateEmailType.getModelJSONString(true, false)%>,
			optDefault :'${emailSet.templatetype}'
		});
		$('#butSave').click(function() {
			var content = CKEDITOR.instances.editor01.getData();
			$("#templatecontent").val(content);
			var exp = $('#form1').validationEngine('validate');
			if(exp==true) {
				if(content == ''){
					alert('<%=MessageResourcesUtils.getMessage(request, "topic.news.contents.alert")%>');
					return ;
				}
				// 显示进度条
				showSending();
				$.ajax({
					type: 'POST',
				    url: '${systemctx}/systemmanage/email/emailSet/updateEmailSet.json',
				    data: $("#form1").serialize(),
				    dataType: 'json',
				    success: function(data) {
				    	//关闭执行...
						closeSending();
						if(data.messagetype == 1){
							alert(data.message);
							parent.document.location.reload();
	    					$.fn.closeIssWindow();
						}else {
							alert(data.message);
							return;							
						}
				    }
				});
			}
		});
		
		var type = '${emailSet.templatetype}';
		if(type != ''){
			$.getJSON('${systemctx}/systemmanage/email/emailSet/getTemplateParamerMap.json?code=' + type,function(data){
				if(data.resposecode == '<%=MessageObject.ResponseCode.code_200%>') {
					var objs = data.object;
					var button = "";
					for(var i = 0;i < objs.length; i ++){
						var obj = objs[i];
						button += '<button type="button" value="'+obj.key+'">' +obj.value+ '</button>&nbsp;&nbsp;';
					}
					$(".smsparameter").html(button);
					bindEvent();
				} else {
					alert(data.message);
					return ;
				}
			});
		}
	
		$("#templatetype").on('change',function(){
			var select_val = $(this).children('option:selected').val();
			if(select_val != '' && select_val != undefined){
				$.getJSON('${systemctx}/systemmanage/email/emailSet/getTemplateParamerMap.json?code=' + select_val,function(data){
					if(data.resposecode == '<%=MessageObject.ResponseCode.code_200%>') {
						var objs = data.object;
						var button = "";
						for(var i = 0;i < objs.length; i ++){
							var obj = objs[i];
							button += '<button type="button" value="'+obj.key+'">' +obj.value+ '</button>&nbsp;&nbsp;';
						}
						$(".smsparameter").html(button);
						bindEvent();
					} else {
						alert(data.message);
						return ;
					}
				});
			} else {
				$(".smsparameter").html('');
			}
		});
		
		$('#butReturn').click(function(){
			parent.document.location.reload();
		    $.fn.closeIssWindow();
		});
		
	});
	
	function bindEvent(){
		$(".smsparameter :button").on('click',function(){
			SetContent($(this).val(),"#editor01");
		});
	}

	function InsertText(value) {
		var oEditor = CKEDITOR.instances.editor1;
		oEditor.insertText(value);
	}
	
	var SetContent = function(value, id) {
		var str = value;
		var oEditor = CKEDITOR.instances.editor01;
		var val = CKEDITOR.instances.editor01.getData();
		CKEDITOR.instances.editor01.setData(val + str);
	};
</script>
<%-- 引入页面尾 --%>
<%@ include file="/pageFloor.jsp"%>